package pers.zb.cloud.service.upms.controller;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pers.zb.cloud.service.upms.entity.SysPermission;
import pers.zb.cloud.service.upms.enums.PermissionStatus;
import pers.zb.cloud.service.upms.service.ClientDetailService;
import pers.zb.cloud.service.upms.service.PermissionService;
import pers.zb.cloud.service.upms.vo.ClientVo;
import pers.zb.cloud.service.upms.vo.JsonResult;
import pers.zb.cloud.service.upms.vo.PermissionVo;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/permission")
public class PermissionController {

	@Autowired
	private ClientDetailService clientDetailService;

	@Autowired
	private PermissionService permissionService;

	/**
	 * 根据父ID获取菜单列表
	 * @param pid
	 * @param clientId 应用的ClientID - 点击左侧应用节点，查询对应的应用的菜单数据
	 * @return
	 */
	@RequestMapping(value = "/list/{pid}")
	@ResponseBody
	public List<PermissionVo> getPermissionListByPid(@PathVariable("pid") Long pid, @RequestParam(value = "clientId",required = false) String clientId) {
		List<PermissionVo> permissionVoList = new ArrayList<PermissionVo>();
		if(StringUtils.isEmpty(clientId)){//菜单列表默认不显示数据，只有当点击左侧应用的时候，才会显示对应的应用菜单列表
			return permissionVoList;
		}
		permissionVoList = permissionService.getPermissionListByPid(pid,clientId);
		return permissionVoList;
	}

	/**
	 * 根据ID获取权限
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}")
	@ResponseBody
	public SysPermission getPermissionById(@PathVariable("id") Long id) {
		return permissionService.getPermissionById(id);
	}


	/**
	 * 判断是否具有子节点
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/children/have/{id}")
	@ResponseBody
	public boolean haveChildrenMenu(@PathVariable("id") Long id) {
		return permissionService.haveChildrenMenu(id);
	}

	/**
	 * 新增权限
	 */
	@RequestMapping(value = "", method = {RequestMethod.POST})
	@ResponseBody
	public JsonResult<String> save(@RequestBody SysPermission sysPermission) {
		JsonResult<String> jsonResult = new JsonResult<>();
		try {
			permissionService.addPermission(sysPermission);
		} catch (Exception e) {
			log.debug("新增 权限 - 失败：" + e.getMessage());
			jsonResult.setCode(500);
			jsonResult.setMsg("error");
			return jsonResult;
		}
		return jsonResult;
	}

	/**
	 * 更新权限
	 * @param sysPermission
	 * @return
	 */
	@RequestMapping(value = "", method = {RequestMethod.PUT})
	@ResponseBody
	public JsonResult<String> update(@RequestBody SysPermission sysPermission) {
		JsonResult<String> jsonResult = new JsonResult<>();
		try {
			permissionService.updatePermission(sysPermission);
		} catch (Exception e) {
			log.debug("更新 权限 - 失败：" + e.getMessage());
			jsonResult.setCode(500);
			jsonResult.setMsg("error");
			return jsonResult;
		}
		return jsonResult;
	}

	/**
	 * 删除菜单
	 */
	@RequestMapping(value = "/batch", method = {RequestMethod.POST})
	@ResponseBody
	public JsonResult<String> delete(@RequestParam(value = "permissionId") String permissionId) {
		JsonResult<String> jsonResult = new JsonResult<>();
		try {
			String[] permissionIdArrString = permissionId.split(",");
			Long[] permissionIdArrs = (Long[]) ConvertUtils.convert(permissionIdArrString,Long.class);
			permissionService.deletePermission(permissionIdArrs);
		} catch (Exception e) {
			log.debug("删除权限 - 失败：" + e.getMessage());
			jsonResult.setCode(500);
			jsonResult.setMsg("error");
			return jsonResult;
		}
		return jsonResult;
	}


	/**
	 * 获取除自身外的其他所有可用的权限
	 * 		在权限添加、编辑的时候，加载显示 父权限 下拉框的内容
	 *
	 * @param id 在添加权限的时候，前端默认传递-1；编辑权限的时候，传递的是具体的权限ID
	 * @param clientId 权限所属的应用
	 */
	@RequestMapping(value = "/notitself/list/{id}/{clientId}")
	@ResponseBody
	public List<PermissionVo> getPermissionListByNotItself(@PathVariable("id") Long id,@PathVariable("clientId") String clientId) {
		List<PermissionVo> permissionVoList = permissionService.getPermissionListByNotItself(id,clientId);
		return permissionVoList;
	}


	/**
	 * 新增权限
	 *
	 * @param clientId 所属应用
	 */
	@RequestMapping(value = "/view/add/{clientId}", method = {RequestMethod.GET})
	public ModelAndView addMenuVew(@PathVariable("clientId") String clientId) {
		ModelAndView modelAndView = new ModelAndView("/sys/permission/add");
		modelAndView.addObject("permissionStatus", PermissionStatus.values());
		modelAndView.addObject("clientId",clientId);
		return modelAndView;
	}


	/**
	 *  获取应用列表 - 左侧tree
	 */
	@RequestMapping(value = "/client/tree/list")
	@ResponseBody
	public List<ClientVo> getClientListTree() {
		List<ClientVo> clientVoList = clientDetailService.getClientListTree();
		return clientVoList;
	}
}
